package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.pojo.ShoppingCart;
import com.example.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        //设置用户Id
        shoppingCart.setUserId(userId);

        //判断此条数据是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //菜品Id不为空则找菜品
        queryWrapper.eq(shoppingCart.getDishId()!=null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //套餐Id不为空则找套餐
        queryWrapper.eq(shoppingCart.getSetmealId()!=null ,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        if (one!=null){
            //存在则数量+1
            Integer number = one.getNumber();

//            此时shoppingCart中没有Id，不能用于update
//            shoppingCart.setNumber(number);
//            shoppingCartService.updateById(shoppingCart);

            one.setNumber(++number);
            shoppingCartService.updateById(one);
        }else {
            //不存在则插入
//            shoppingCartService.save(shoppingCart);  //数量数据库默认是1

            //确保one中有数据，方便返回前端
            one = shoppingCart;
            one.setNumber(1);   //即使数据库有默认值，但因为要返回前端，所以仍要设值
            shoppingCartService.save(one);
        }

        return R.success(one);
    }

    /**
     * 减少购物车中菜品或套餐的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();

        //查出该条数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        queryWrapper.eq(shoppingCart.getDishId()!=null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        shoppingCart  = shoppingCartService.getOne(queryWrapper);

        //获取当前数量
        Integer number = shoppingCart.getNumber();

        //数量减1
        shoppingCart.setNumber(--number);

        //如果数量大于0，则更新
        if (number > 0){
            shoppingCartService.updateById(shoppingCart);

        }
        //如果等于0，则删除该条数据
        else if (number == 0){
            shoppingCartService.removeById(shoppingCart.getId());
        }
        else {
            return R.error("数量出错");
        }

        return R.success(shoppingCart);
    }



    //显示当前购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }
}
